package com.hqd.ch03.v35.web.servlet.support;

import com.hqd.ch03.v35.web.ui.ModelMap;

import java.util.Collection;
import java.util.Map;

public class RedirectAttributesModelMap extends ModelMap implements RedirectAttributes {


    private final ModelMap flashAttributes = new ModelMap();


    public RedirectAttributesModelMap() {

    }

    @Override
    public Map<String, ?> getFlashAttributes() {
        return this.flashAttributes;
    }

    @Override
    public RedirectAttributesModelMap addAttribute(String attributeName, Object attributeValue) {
        super.addAttribute(attributeName, formatValue(attributeValue));
        return this;
    }


    private String formatValue(Object value) {
        if (value == null) {
            return null;
        }
        return value.toString();
    }

    /**
     * {@inheritDoc}
     * <p>Formats the attribute value as a String before adding it.
     */
    @Override
    public RedirectAttributesModelMap addAttribute(Object attributeValue) {
        super.addAttribute(attributeValue);
        return this;
    }

    /**
     * {@inheritDoc}
     * <p>Each attribute value is formatted as a String before being added.
     */
    @Override
    public RedirectAttributesModelMap addAllAttributes(Collection<?> attributeValues) {
        super.addAllAttributes(attributeValues);
        return this;
    }

    /**
     * {@inheritDoc}
     * <p>Each attribute value is formatted as a String before being added.
     */
    @Override
    public RedirectAttributesModelMap addAllAttributes(Map<String, ?> attributes) {
        if (attributes != null) {
            attributes.forEach(this::addAttribute);
        }
        return this;
    }

    /**
     * {@inheritDoc}
     * <p>Each attribute value is formatted as a String before being merged.
     */
    @Override
    public RedirectAttributesModelMap mergeAttributes(Map<String, ?> attributes) {
        if (attributes != null) {
            attributes.forEach((key, attribute) -> {
                if (!containsKey(key)) {
                    addAttribute(key, attribute);
                }
            });
        }
        return this;
    }

    @Override
    public Map<String, Object> asMap() {
        return this;
    }

    /**
     * {@inheritDoc}
     * <p>The value is formatted as a String before being added.
     */
    @Override
    public Object put(String key, Object value) {
        return super.put(key, formatValue(value));
    }

    /**
     * {@inheritDoc}
     * <p>Each value is formatted as a String before being added.
     */
    @Override
    public void putAll(Map<? extends String, ? extends Object> map) {
        if (map != null) {
            map.forEach((key, value) -> put(key, formatValue(value)));
        }
    }

    @Override
    public RedirectAttributes addFlashAttribute(String attributeName, Object attributeValue) {
        this.flashAttributes.addAttribute(attributeName, attributeValue);
        return this;
    }

    @Override
    public RedirectAttributes addFlashAttribute(Object attributeValue) {
        this.flashAttributes.addAttribute(attributeValue);
        return this;
    }

}
